﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace NougakuDoController
{
    static class Logging
    {

        static string logname = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name + ".txt";

        static string GetPath()
        {
            return AppDomain.CurrentDomain.BaseDirectory;
        }

        static public Tuple<string, string> GetLogFileName()
        {
            return new Tuple<string, string>(GetPath(), logname);
        }

        static public void Write(string msg, LogType mType)
        {
            var message = BuildMsg(msg, mType);
            Console.WriteLine(message);
            using (var st = File.Open(Path.Combine(GetPath(), logname), FileMode.Append))
            {

                using (var tw = (TextWriter)new StreamWriter(st))
                {
                    tw.WriteLine(BuildMsg(msg, mType));

                    tw.Close();
                }
                st.Close();
            }
        }

        static public void Write(string format,object[] msg, LogType mType)
        {
            var message = BuildMsg(String.Format(format, msg), mType);
            Console.WriteLine(message);

            using (var st = File.Open(Path.Combine(GetPath(), logname), FileMode.Append))
            {

                using (var tw = (TextWriter)new StreamWriter(st))
                {
                    tw.WriteLine(message);

                    tw.Close();
                }
                st.Close();
            }
        }

        static string BuildMsg(string msg, LogType mType)
        {
            StringBuilder buffer = new StringBuilder();
            buffer.Append(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
            buffer.Append("\t");
            buffer.Append(mType.ToString());
            buffer.Append("\t");
            buffer.Append(msg);
            return buffer.ToString();
        }

        public enum LogType
        {
            INFO,
            ERROR,
            WARNING
        }
    }
}
